/**
 * <b>多文件按顺序下載</b>
 *
 */
package com.addcn.common.utils{
	import flash.display.Loader;
	import flash.events.EventDispatcher;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;	
	import flash.utils.Dictionary;	
	
	import com.addcn.common.inter.Iloader;	
	import com.addcn.common.event.DataEvent;

	public class  QuenceLoader extends GameLoader implements Iloader
	{
		private var progress:uint;
		private var complete:uint;
		private var ioError:String;
		private var timerOut:String;
		
		private var _inverted :Boolean ;
		
		public function QuenceLoader(r_domain:ApplicationDomain)
		{			
			super(r_domain);			
		}

		
		/**
		 * 设置下载顺序
		 * @param	r_inverted Boolean 为ture则按队列倒序下载
		 */
		public function loadInverted(r_inverted:Boolean = true) : void {
			this._inverted = r_inverted;
		}

		/**
		 * 开始执行下载
		 * 
		 */
		override public function start():void 
		{				
			var _lot_1:Object;					
			
			if ( loaderArr.length == 0 ) {				
				return;
			}				
						
			if(!this._inverted){
				_lot_1  = loaderArr.shift();
			}else {
				_lot_1  = loaderArr.pop();
			}
			complete = loaderArr.length as uint;	
			var _lot_url:String = _lot_1.url;
			if ( "type" in _lot_1.param && _lot_1.param.type == 'xml') {				
				loadXML(_lot_url, _lot_1.param);
			}else {
				loadFile(_lot_url, _lot_1.param);								
			}
	
		}
		/**
		 * 文件(swf,png,gif,jpg,jepg,mp3,mp4.......)下載並開始廣播
		 * @param	param1	文件路徑
		 * @param	param2	文件屬性
		 */
		override public function loadFile(param1:String, param2:Object = ''):void {
			var loader:Loader;
			var onComplete:Function;
			var onProgress:Function;
			var onIoError:Function;			
			onComplete = function(evt) {					
				file[param2.id] = loader;				
				var _com:Object = { name:param2.name, com: complete};
				dispatchEvent(new DataEvent(COMPLETE, _com));				
				start();
			}
			onProgress = function(evt) {
				var _lot_1:Object;
				_lot_1 = { loaded:evt.bytesLoaded , total:evt.bytesTotal };
				dispatchEvent(new DataEvent(PROGRESS,_lot_1));				
			}
			onIoError = function(evt) {		
				dispatchEvent(new DataEvent(IO_ERROR));
			}
			loader = new Loader();			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);			
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);	
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			loader.load(new URLRequest(param1), new LoaderContext(false, fogDomain));
		}
		/**
		 * 加載其他的xml文件
		 * @param	param1
		 * @param	param2
		 */
		override public function loadXML(param1:String, param2:Object = '' ):void {
			var loader:URLLoader;
			var onComplete:Function;
			var onProgress:Function;
			var onIoError:Function;
			onComplete = function(evt) {
				start();
				file[param2.id] = XML(loader.data);
				var _com:Object = { name:param2.name, com: complete};
				dispatchEvent(new DataEvent(COMPLETE, _com));
			}
			onProgress = function(evt) {
				return;
			}
			onIoError = function(evt) {
				return;
			}			
			loader = new URLLoader();			
			loader.addEventListener(Event.COMPLETE, onComplete);			
			loader.addEventListener(ProgressEvent.PROGRESS, onProgress);	
			loader.load(new URLRequest(param1));
		}

	}

}
